using System;
using System.Collections.Generic;
using UnityEngine;

namespace UFrame
{
    /// <summary>
    /// 处理屏幕后期的效果
    /// </summary>
    public class UGUIPostProcessing : MonoBehaviour
    {
        public Material effect;
        private Material instanceEffect;

        [SerializeField] public List<MaterialProcessData> valList = new List<MaterialProcessData>();

        private void Awake()
        {
            SetValue();
        }

        public void SetValue()
        {
            if (effect == null)
            {
                return;
            }

            if (instanceEffect == null)
            {
                instanceEffect = new Material(effect);
            }

            foreach (var item in valList)
            {
                if (instanceEffect.HasProperty(item.name))
                {
                    if (item.type == EMaterialProcessData.Vector4 || item.type == EMaterialProcessData.Vector3
                                                                  || item.type == EMaterialProcessData.Vector2)
                    {
                        instanceEffect.SetVector(item.name, item.v4);
                    }

                    if (item.type == EMaterialProcessData.Color)
                    {
                        instanceEffect.SetColor(item.name, item.color);
                    }

                    if (item.type == EMaterialProcessData.Float)
                    {
                        instanceEffect.SetFloat(item.name, item.f1);
                    }

                    if (item.type == EMaterialProcessData.Int)
                    {
                        instanceEffect.SetFloat(item.name, item.intValue);
                    }
                }
            }
        }


        //[ImageEffectOpaque]
        private void OnRenderImage(RenderTexture source, RenderTexture destination)
        {
            if (instanceEffect == null)
            {
                Graphics.Blit(source, destination);
            }
            else
            {
                Graphics.Blit(source, destination, instanceEffect);
            }
        }
    }

    [Serializable]
    public class MaterialProcessData
    {
        public string name;
        public EMaterialProcessData type;
        public string value;

        public Color color;
        public Vector4 v4;
        public float f1;
        public int intValue;
    }

    [Serializable]
    public enum EMaterialProcessData
    {
        Color,
        Vector3,
        Vector2,
        Vector4,
        Float,
        Int,
    }
}